package cn.edu.nit.sys.mapper;

import cn.edu.nit.sys.entity.Role;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface RoleMapper extends BaseMapper<Role> {

    @Select("select r.id, any_value(r.role) as role, any_value(r.name) as name, group_concat(any_value(m.label)) as menus " +
            "from sys_role r " +
            "left join sys_role_menu rm on rm.role_id = r.id " +
            "left join sys_menu m on m.id = rm.menu_id " +
            "where r.name like concat('%', concat(#{name}, '%')) group by id")
    @Results(value = {
            @Result(column = "id", property = "id"),
            @Result(column = "role", property = "role"),
            @Result(column = "name", property = "name"),
            @Result(column = "menus", property = "menus"),
            @Result(column = "id", property = "count", one = @One(select = "cn.edu.nit.sys.mapper.RoleMapper.count"))
    })
    List<Role> getRoleList(String name);

    @Select("select count(1) from sys_user where role_id = #{id}")
    Integer count(Integer id);

    @Insert("insert into sys_role(role, name, menus) values(#{role.role}, #{role.name}, #{menuStr})")
    Integer addRole(Role role, String menuStr);
}
